package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import java.net.URLEncoder;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.User;
import com.example.springboot.service.IItemService;
import com.example.springboot.service.ITypeService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Result;
import org.springframework.web.multipart.MultipartFile;
import com.example.springboot.entity.Item;

import org.springframework.web.bind.annotation.RestController;

/**
* ItemAPI接口
*/
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private ITypeService typeService;

    @GetMapping("/recommend")
    public Result recommend() {
        return Result.success(itemService.recommend().stream().distinct().limit(4).collect(Collectors.toList()));
    }

    @GetMapping("/type/{id}")
    public Result type(@PathVariable Integer id) {
        return Result.success(itemService.list(new LambdaQueryWrapper<Item>().eq(Item::getTypeId,id)));
    }


    @PostMapping
    public Result save(@RequestBody Item item) {
        User currentUser = TokenUtils.getCurrentUser();
        item.setUser(currentUser.getName());
        item.setTime(DateUtil.now());
        itemService.save(item);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Item item) {
        itemService.updateById(item);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        itemService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        itemService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(itemService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        Item item = itemService.getById(id);
        item.setType(typeService.getById(id));
        return Result.success(item);
    }

    @GetMapping("/page/type")
    public Result findPageType(@RequestParam(defaultValue = "") String name,
                               @RequestParam Integer typeId,
                               @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<Item>().orderByDesc("id");
        queryWrapper.like(!"".equals(name), "name", name);
        queryWrapper.eq(Objects.nonNull(typeId), "type_id", typeId);
        Page<Item> page = itemService.page(new Page<>(pageNum, pageSize), queryWrapper);
        page.getRecords().stream().forEach(item -> {
            item.setType(typeService.getById(item.getTypeId()));
        });
        return Result.success(page);
    }


    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<Item>().orderByDesc("id");
        queryWrapper.like(!"".equals(name), "name", name);
        Page<Item> page = itemService.page(new Page<>(pageNum, pageSize), queryWrapper);
        page.getRecords().stream().forEach(item -> {
            item.setType(typeService.getById(item.getTypeId()));
        });
        return Result.success(page);
    }

}
